package com.yyw.Service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.Share;
import com.yyw.dao.CollectDao;
import com.yyw.dao.ShareDao;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

@Service
public class ShareService {

    @Autowired
    private ShareDao shareDao;

    @Autowired
    private CollectDao collectDao;

    public Result getShare(Long id, Long userid) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        Share share = shareDao.getShareById(id);
        if (userid != null && share != null) {
            int i = collectDao.getCollectState(userid, id);
            if (i > 0) {
                share.setIsCollect(true);
            }
        }
        return ResultFactory.success(share);
    }

    public Result deleteShare(Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = shareDao.delereShare(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    public Result saveShare(Share share) {
        // 数据校验
        if (StringUtils.isEmpty(share.getTitle()) || StringUtils.isEmpty(share.getContent())) {
            return ResultFactory.notice("数据不合法");
        }
        if (share.getLevel() == null || share.getLevel() < 1 || share.getLevel() > 5) {
            return ResultFactory.notice("数据不合法");
        }
        if (share.getUserid() == null) {
            return ResultFactory.notice("数据不合法");
        }

        int i = shareDao.saveShare(share);
        if (i > 0) {
            return ResultFactory.success("添加成功");
        } else {
            return ResultFactory.failure("保存失败");
        }
    }

    public Result getShares(Integer level, Integer pageNo, Integer pageNum, int t, String search) {
        // 数据校验
        if (level != null && (level < 0 || level > 5)) {
            return ResultFactory.notice("难度不合法");
        }
        if (pageNo == null || (pageNo < 1)) {
            return ResultFactory.notice("页数不合法");
        }
        if (pageNum == null || pageNum < 1) {
            return ResultFactory.notice("每页数量不合法");
        }

        if (!StringUtils.isEmpty(search)) {
            search = "%" + search + "%";
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Share> shares = shareDao.getShares(t, level, search);

        PageInfo<Share> pageInfo = new PageInfo<>(shares);
        return ResultFactory.success(pageInfo);
    }

    public Result getUserShares(Integer pageNo, Integer pageNum, Long userid) {
        // 数据校验
        if (pageNo == null || (pageNo < 1)) {
            return ResultFactory.notice("页数不合法");
        }
        if (pageNum == null || pageNum < 1) {
            return ResultFactory.notice("每页数量不合法");
        }

        if (userid == null) {
            return ResultFactory.notice("数据不合法");
        }

        PageHelper.startPage(pageNo, pageNum);
        List<Share> shares = shareDao.getUserShares(userid);

        PageInfo<Share> pageInfo = new PageInfo<>(shares);
        return ResultFactory.success(pageInfo);
    }

}
